package com.open.party.cloud.view.home.jiCengDangJian.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.open.party.cloud.R;
import com.open.party.cloud.model.ArticleEntity;
import com.sinothk.android.utils.XUtils;
import com.sinothk.view.image.rounded.RoundedImageView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import com.sinothk.android.utils.inters.OnSuperListener;

public class PartyServiceMainListAdapter extends RecyclerView.Adapter<PartyServiceMainListAdapter.ViewHolder> {

    // 展示数据
    private ArrayList<ArticleEntity> mData;
    private Context mContext;
    private OnSuperListener<ArticleEntity> onItemClickListener;

    public PartyServiceMainListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setOnSuperListener(OnSuperListener<ArticleEntity> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setData(List<ArticleEntity> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<ArticleEntity> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.party_service_main_list_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // 绑定数据
        final ArticleEntity itemData = mData.get(position);

        holder.titleTv.setText(XUtils.string().getNotNullValue(itemData.getTheme()));
        holder.typeTv.setText(XUtils.string().getNotNullValue(itemData.getPublishingDepartment(), "未知"));

        holder.browseTimesTv.setText("" + itemData.getBrowseTimes());
//        holder.alreadyClickTv.setText("" + itemData.getClickCount());

        if (itemData.getReleaseTime() == null) {
            holder.timeTv.setText("近期");
        } else {
            String dateTimeStr = XUtils.date().getDateStrByDate(itemData.getReleaseTime(), "yyyy-MM-dd");
            holder.timeTv.setText(String.valueOf(dateTimeStr));
        }

        Picasso.get().load(itemData.getImgUrl())
                .error(R.drawable.default_img)
                .placeholder(R.drawable.default_img)
                .into(holder.imageView);

        if (onItemClickListener != null) {
            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onClick(position, itemData, "");
                }
            });
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout rootView;
        TextView titleTv, timeTv, typeTv, browseTimesTv, alreadyClickTv;
        RoundedImageView imageView;

        ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);

            titleTv = itemView.findViewById(R.id.titleTv);
            timeTv = itemView.findViewById(R.id.timeTv);
            typeTv = itemView.findViewById(R.id.typeTv);
            browseTimesTv = itemView.findViewById(R.id.browseTimesTv);
            alreadyClickTv = itemView.findViewById(R.id.alreadyClickTv);

            imageView = itemView.findViewById(R.id.imageView);
        }
    }
}